home *** CD-ROM | disk | FTP | other *** search
/ Commodore Free 10 / Commodore_Free_Issue_10_2007_Commodore_Computer_Club.d64 / t.hexfiles 5 < prev    next >
File List  |  2023-02-26  |  8KB  |  323 lines

  1. u           HEX files Part 5
  2.             By Jason Kelk
  3.        www.Oldschool-Gaming.com
  4.  
  5. Righto, time for some more code
  6. writing entertainment! Now, where were
  7. we?
  8.  
  9. Ah yes, the challenge from the
  10. previous instalment; your mission, if
  11. you decided to accept it was to change
  12. the text colour to light blue and the
  13. sprite colour to dark blue. The text
  14. colour is altered by changing the LDA
  15. #$0F on line 25 of the source to LDA
  16. #$0E or indeed any colour you want. To
  17. change the sprites look at line 62. It
  18. reads LDA #$0C and to make them purple
  19. use LDA #$04. All sorted now? Good,
  20. lets get back to dissecting the
  21. remaining code, starting from line
  22.  
  23. main lda #$fc
  24. rashold cmp
  25. $d012bne rashold
  26.  
  27. Now our friend the raster has been
  28. introduced to us a couple of
  29. installments back, and the principle
  30. remains the same; the C64 projects the
  31. screen to the TV a line at a time and
  32. this loop waits for line $FC, which is
  33. just off the bottom of the standard
  34. screen. From here onwards the term
  35. "frame" will denote one complete scan
  36. of the screen by the raster, so one
  37. frame is a 50th of a second.
  38.  
  39. ldx scrlcount
  40. inx
  41. stx scrlcount
  42. cpx #$08
  43. bne dontmove
  44.  
  45. Right, we've already discussed using
  46. labels as places to keep numbers.
  47. scrlcount is our smooth scroll
  48. counter, it's counting how many frames
  49. have passed up to a maximum of eight
  50. to delay our scroller since moving it
  51. every frame would be far too fast to
  52. read. It's also used to work out what
  53. value should be in $D016 later on, but
  54. I'll explain that when we get there.
  55.  
  56. ldx #$00
  57. scrlmove
  58. lda $05e1,x
  59. sta $05e0,x
  60. inx
  61. cpx #$27
  62. bne scrlmove
  63. ldx messcount
  64. lda scrolltext,x
  65. sta $0607
  66. inx stx
  67. messcount
  68.  
  69. Well, this section has covered before
  70. too and it's basically the same scroll
  71. shifting routine we had moving so
  72. quickly previously. This time,
  73. however, we are reading from our own
  74. message in the computers memory at
  75. wherever the label scrolltext is. That
  76. gets defined later, don't worry.
  77.  
  78. ldx #$00
  79. stx scrlcount
  80.  
  81. Because we've just finished a "move"
  82. of the scroller we now have to reset
  83. our counter so that the machine will
  84. wait another eight frames before doing
  85. it again. Okay, now earlier we
  86. branched to dontmove if we were not
  87. going to move the scroll, so lets see
  88. where that leads us.
  89.  
  90. dontmove
  91. txa
  92. eor #$07
  93. sta $d016
  94.  
  95. Okay, this is just a little bit of
  96. trickery to set the smooth scrolling
  97. up for our message. TXA we have
  98. already covered, but EOR is new. EOR
  99. (or Exclusive OR) is what is known as
  100. a logic gate. There are three
  101. regularly used gates, ORA (known as
  102. just an OR gate, but called ORA
  103. because all 6510 commands are three
  104. letters long, so we add an A for
  105. accumulator), EOR and AND. ORA works
  106. like this:
  107.  
  108. LDA #$64 or in binary %01100100
  109.  
  110. ORA #$C7 or in binary %11000111
  111.  
  112. Leaves A as #$E7 or in binary
  113. %11100111
  114.  
  115. Why? Well, look at the first column of
  116. binary numbers. The first two are a 0
  117. in the top row and a 1 in the second
  118. and the "rule" is that if either the
  119. first OR the second number (or indeed
  120. both) is a 1 the answer, in the third
  121. number in the column will also be 1.
  122. If both numbers are a 0 the answer
  123. will be a 0, just like the fourth and
  124. fifth columns.
  125.  
  126. AND works on a similar principle,
  127. except that the first number AND the
  128. second number must be a 1 for the
  129. outcome to be a 1, otherwise the
  130. answer is 0. If only one of the
  131. numbers is a 1 then the answer is a 0,
  132. meaning that if the ORA above is
  133. replaced with an AND the binary answer
  134. will be %01000100.
  135.  
  136. Now for our little friend EOR. The
  137. result of our above example after
  138. using EOR instead of ORA would be
  139. %10100011 because EOR works by a
  140. different means. Again, it compares
  141. the columns separately, but if the
  142. second number is a 1 it takes the
  143. first number and toggles it, changes
  144. it from a 0 to a 1 or vice versa. If
  145. the content of the second number is a
  146. 0 nothing changes, and the content of
  147. the first passes straight through. So
  148. the result of EOR #$07 on our counter,
  149. which runs from $00 to $07 is to
  150. basically invert it so that it goes
  151. from $07 to $00 and we can then put
  152. this into $D016 for the smooth
  153. scrolling.
  154.  
  155. Okay, so now we've sorted out the
  156. scrolling and the smooth scroll, it's
  157. time for the sprite positions.
  158.  
  159. ldx #$00
  160. ldy sineposx
  161. setsprx lda
  162. sineDcurve,y
  163. sta $d000,x
  164. tya
  165. clc
  166. adc #$20
  167. tay
  168. inx
  169. inx
  170. cpx #$10
  171. bne setsprx
  172.  
  173. Right, this is a fairly normal copy
  174. loop (as we've done before) but with
  175. an unusual twist. Before I explain how
  176. it works, I'll just quickly explain
  177. sine and cosine curves a bit more,
  178. which are used when demo programmers
  179. want to make things swing back and
  180. forth. At $0E00 and $0F00 are two
  181. tables of data, each 256 ($100) bytes
  182. long. If you were to examine the data
  183. you'd find that it contains the
  184. co-ordinates for a sprite, starting
  185. from the right hand side of the area
  186. it covers, accelerating towards the
  187. middle and decelerating at the left
  188. hand side of the area, then moving
  189. back in the same way.
  190.  
  191. The above loop reads the curve at
  192. $0E00 using the Y (which contains
  193. another counter) and puts it into
  194. $D000. It then adds a value of $20 to
  195. the Y, increments X twice (so that it
  196. only writes to the sprite X values,
  197. $D000, $D002 and so on) and then, if
  198. it hasn't reached $10 and therefore
  199. done all eight sprites goes back.
  200.  
  201. Why do we add $20? Well, it's to make
  202. all six sprites read from different
  203. points on the curve to produce that
  204. swirling effect. Try changing that
  205. value to $00 and they all appear with
  206. the same X position.
  207.  
  208. ldx #$00
  209. ldy sineposy
  210. setspry
  211. lda sineDcurveD2,y
  212. sta $d001,x
  213. tya
  214. clc
  215. adc #$1c
  216. tay
  217. inx
  218. inx
  219. cpx #$10
  220. bne setspry
  221.  
  222. This is basically the same loop as
  223. we've just looked at, except that we
  224. are using sineposy, reading from $0F00
  225. (the vertical sine table, slightly
  226. different to the horizontal one) and
  227. writing the values to $D001, $D003 and
  228. so forth for the vertical sprite
  229. positions. The ADC #$1C is different
  230. to the previous loop merely to make
  231. the curves a bit more interesting. It
  232. can quite happily be $20 or indeed any
  233. other value, so why not try playing
  234. with it and its fellow to see what you
  235. can do?
  236.  
  237. lda sineposx
  238. sec
  239. sbc #$03
  240. sta
  241. sineposx
  242. lda sineposy
  243. sec
  244. sbc #$02
  245. sta sineposy
  246.  
  247. Okay, this just moves the counters
  248. around for the sprite movement. Again,
  249. the $03 and $02 can be altered to
  250. change how fast the sprites swing
  251. around, a value of $00 will cause them
  252. to stop dead and a value of $FF will
  253. make them go backwards on one axis
  254. slowly.
  255.  
  256. jsr $1003
  257. jmp main
  258.  
  259. As explained last issue we are using
  260. an external music routine. Every frame
  261. we have to call the routine to keep
  262. the music playing and here's the JSR
  263. call for the tune we're using. And the
  264. JMP completes our loop, as we go back
  265. to main and wait for raster position
  266. $FC on the next frame.
  267.  
  268. * = $2000
  269. message .scrl "welcome to the
  270. scrolling message! "
  271. .scrl "this little demo-ette was coded
  272. for " .
  273.  
  274. And to finish off this block of code,
  275. here's the previously mentioned
  276. labelmessage and another new assembler
  277. directive to go with it. The .scrl
  278. command simply writes the text in
  279. quotes to the memory (at $2000, as
  280. specified by the * command above) in a
  281. format our routine can use, the letter
  282. A is represented as $01, B is $02 and
  283. so on. The text included in the source
  284. (of which only a part is reproduced
  285. here) is exactly 256 bytes long
  286. because the scroll routine can't
  287. handle any more or indeed less.
  288.  
  289. Right, all done and this time I don't
  290. have a challenge for you, just some
  291. "play" suggestions. The values in the
  292. two sprite setup loops ($20 and $1C)
  293. and the mover ($03 and $02) can be
  294. altered.
  295.  
  296. Why not try putting different values
  297. in and seeing what happens to the
  298. sprites. Some interesting ones to try
  299. are replacing $20 with $82, which
  300. causes each sprite in turn to read its
  301. position from opposite ends of the
  302. curve, or replacing $03 and $02 with
  303. $04 and $05. Go on, have a play with
  304. the numbers and I'll see you next time
  305. with something new to look at! As
  306. always, email me if you have any
  307. queries, comments or suggestions.
  308.  
  309. The source code for the routines above
  310. (they're the same as Hex Files 4) can
  311. be downloaded here
  312.  
  313. www.Oldschool-Gaming.com/files/c64/hex
  314. Dfiles/partD6Dfiles.zip
  315.  
  316. Jason Kelk
  317. RE-printed with the permission of the
  318. copyright holder
  319. www.Oldschool-Gaming.com
  320.  
  321.  
  322. ...end...
  323.  
  324.